//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-4-17
// @Time:下午4:49
//

#ifndef GEEJOAN_INTERNALTYPES_H
#define GEEJOAN_INTERNALTYPES_H

// 颜色
#define RESET   "\033[0m"
#define BLACK   "\033[30m"      /* Black */
#define RED     "\033[31m"      /* Red */
#define GREEN   "\033[32m"      /* Green */
#define YELLOW  "\033[33m"      /* Yellow */
#define BLUE    "\033[34m"      /* Blue */
#define MAGENTA "\033[35m"      /* Magenta */
#define CYAN    "\033[36m"      /* Cyan */
#define WHITE   "\033[37m"      /* White */
#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */

#include <vector>
#include <map>
namespace GeeJoan{
    namespace Common{
        enum ErrorCode
        {
            ERROR_SUCCESS = 0,
            ERROR_FAIL = -1,
        };

        struct SensorDataType
        {
            int64_t uid; // 用户
            uint64_t GatewayId; // 网关id
            uint64_t DeviceId; // 终端id
            int32_t permission;
            int32_t key; // key
            std::string data; // 具体数据,透传
            int64_t modifyat;
            int64_t create;
        };

        struct DeviceDataType
        {
            int64_t uid; // 用户
            uint64_t GatewayId; // 网关id
            uint64_t DevideId; // 终端id
            int32_t permission;
            std::string Note;
            std::string Token;
            int64_t modifyat;
            int64_t create;
        };

        enum NETWORK_MOD_TYPE
        {
            NETWORK_MOD_TYPE_AUTH = 1,
            NETWORK_MOD_TYPE_IOT = 2,
        };

        enum AUTH_MOD_TYPE
        {
            AUTH_REGISTER = 1,
            AUTH_LOGIN = 2,
            AUTH_LOGOUT = 3,
            AUTH_CHANGE_PASSWORD = 5,
        };

        enum IOT_MOD_TYPE
        {
            IOT_UPLOAD_SENSOR_DATA = 1,
            IOT_FIND_SENSOR_DATA = 2,
            IOT_CONTROL_DEVICE = 3,
            IOT_CONTROL_ACK = 4,
            IOT_CONTROL_ACK_LISTERNER = 5,
            IOT_CONTROL_LISTERNER = 6,
            IOT_LOGIN_DEVICE = 7,
            IOT_HEART_BEAT = 8,
            IOT_FIND_GATEWAY = 9,
            IOT_CREATE_DEVICE = 10,
            IOT_FIND_DEVICE_TOKEN = 11,
            IOT_GENERATE_DEVICE_TOKEN = 12,
            IOT_FIND_MY_DEVICE = 13,
        };

    }


    namespace DataBase{
        /**
         * @brief 用户信息数据接口
         */
        class UserDBImpl{
            virtual  int initDataBase() = 0 ;

            virtual  int add_user(std::string username ,std::string password , std::string email) = 0 ;

            virtual  int find_user(std::string username ,std::string password , std::string email = "" ) = 0;

            virtual  int delete_user(std::string username ) = 0 ;

            virtual bool  exist_user(std::string username ) = 0 ;

            virtual int updata_password(std::string username , std::string oldPassword , std::string newPasswotd ) = 0 ;

            virtual int change_password(std::string username , std::string newPassword) = 0 ;
        };

        /**
         * @brief 设备信息结构
         */
        struct DeviceDataType{
            int64_t uid ;
            uint64_t gatewayId ;
            uint64_t  deviceid ;
            int32_t permission ;
            std::string note;
            std::string token;
            int64_t modifyat;
            int64_t create;
        };

        /**
         * @brief 设备管理相关接口
         */
        class DeviceDBImpl{
            virtual int loginDevice(std::string deviceToken) = 0 ;

            // Getaway
            virtual int add_gateway_to_user(int64_t uid, int64_t gid) = 0 ;
            virtual bool exist_gateway( int64_t gid) = 0 ;
            virtual int find_gateways_by_userid(int64_t uid, std::vector<uint64_t> &outGids) = 0 ;
            virtual int add_or_find_gateway(int64_t uid, std::vector<uint64_t> &outGids) = 0 ;
            virtual int find_userid_by_gatewayid(int64_t &output_uid ,int64_t gid) = 0 ;

            // device
            virtual  int find_devices(int64_t uid , int64_t gid , std::vector<int64_t> deviceid ) = 0 ;
            virtual int add_device(uint64_t did , DeviceDataType &deviceData) = 0 ;
            virtual int add_devices(std::map<uint64_t , uint64_t> &gid_did_map , std::vector<DeviceDataType> &deviceinfos) = 0 ;
            virtual int find_devicedata(int64_t did ,  DeviceDataType &deviceData) = 0 ;
            virtual int find_devices_by_gatewayid(std::vector<uint64_t> &deviceids ,uint64_t gid) = 0 ;
            virtual int find_device(DeviceDataType &deviceDataType , uint64_t GatewayId, uint64_t DeviceId) = 0 ;
        };

        /**
         * @brief 缓存相关接口
         */
        class CacheKVImpl{
            virtual bool checkMyDevice(int64_t uid, uint64_t gatewayId, uint64_t devideId) = 0 ;
            virtual bool checkMyGateWay(int64_t uid, uint64_t gatewayId) = 0 ;
            virtual bool checkUserLogin(int64_t &uid , std::string &token) = 0 ;
            virtual bool checkDeviceLoginStatus(std::string &deviceToken) = 0 ;

        };

        /**
         * @brief 传感器数据接口
         */
        struct SensorDataType{
            uint64_t gid;
            uint64_t did;
            int32_t permission;
            int32_t key ;
            std::string data ;
            int64_t modifyat;
            int64_t create;
        };
        class SensorDataDBImpl{
            virtual  int add_data(SensorDataType &data) = 0 ;
            virtual  int add_datas(std::vector<SensorDataType> &datas) = 0 ;
            virtual int find_datas(std::vector<SensorDataType> &datas , int64_t uid, uint64_t GatewayId = 0, uint64_t DevideId = 0,
                                   int32_t permission = 0, int32_t key = 0, int limit = 0, int direction = 0, int64_t targetTime = 0) = 0;



        };
    }
}


#endif //GEEJOAN_INTERNALTYPES_H
